using UnityEngine;
using System.Collections;

public class GameVariables : MonoBehaviour {
	
	public static int currentPlayer = 0;		
	public static int towersPlayer0 = 4;
	public static int towersPlayer1 = 4;
	public static int axisX = 500;	
	public static int axisY = 500;	
	public static int whichSensor = 5;
	public static int launch = 0;
	
	// SPEED RANGES FROM 0 TO 1000 IN THE PHIDGET, BUT 0 TO 50 IN OUR PROGRAM
	public static float speed0 = 25f;  
	public static float speed1 = 25f;
	
	public GameObject bombPrefab;
	
	private bool wait = false;
	private float count = 0;
	
	public Texture speedBackground;
	public Texture speedProgress;

	
	void OnGUI() {			
       	GUI.Button(new Rect(10,10,110,25), "TURN: player " + (currentPlayer + 1));
		GUI.Button(new Rect(280,20,100,20), "Speed");
		
		if (currentPlayer == 0) {
			GUI.Button(new Rect(10,50,200,25), "Remaining towers: " + towersPlayer0);
			drawSpeed(speed0);
		}
		else {
			GUI.Button(new Rect(10,50,200,25), "Remaining towers: " + towersPlayer1);
			drawSpeed(speed1);
		}
		
		if (towersPlayer0 == 0 || towersPlayer1 == 0) {
			if (towersPlayer0 == 0) DrawGameOver(1);
			else DrawGameOver(0);
		}
		
		else {
			playGame();
		}
		
	}
	
	private void playGame() {
		GameObject currentCannon;
		float currentSpeed; 
		if (currentPlayer == 0) {
			currentCannon = GameObject.Find("Cannon0");
			currentSpeed = speed0;
		}
		else {
			currentCannon = GameObject.Find("Cannon1");
			currentSpeed = speed1;
		}
				
		if (wait == true) {
			++count;
			if (count == 600) {
				count = 0;
				wait = false;	
				changeTurn();
			}
		}
		
/////////////////////////////////////////////////////////////////////////////////////////////
////////																		/////////////
////////				C O D E    F O R    T H E    P H I D G E T  			/////////////
////////																		/////////////
/////////////////////////////////////////////////////////////////////////////////////////////
		
		#region PHIDGET	CODE
		  
		//	LAUNCH BOMB  
		if (wait == false && whichSensor == 3 && launch > 100) { 
			Transform bombSpawn = currentCannon.transform.Find("Barrel").transform.Find("bombSpawn");
			GameObject bomb = Instantiate(bombPrefab, bombSpawn.position, bombSpawn.rotation) as GameObject;
			bomb.rigidbody.AddForce(bombSpawn.forward * currentSpeed, ForceMode.VelocityChange);
			wait = true;
			whichSensor = 5;
		}
		
		//	VERTICAL MOVEMENT
		if (whichSensor == 0)
		{		
			float rotationValue = -(float)axisY/1000;
			Transform barrel = currentCannon.transform.Find("Barrel");					
			Quaternion q = new Quaternion(rotationValue-0.25f,
				barrel.localRotation.y,barrel.localRotation.z,
				barrel.localRotation.w);
			barrel.localRotation = q;
			
			whichSensor = 5;
		}
		
		//	HORIZONTAL MOVEMENT
		if (whichSensor == 1)
		{	
			float rotationValue = (float)axisX/1000;
			if (currentPlayer == 1) {
				rotationValue = 1-rotationValue;
			}			
			Quaternion q = new Quaternion(currentCannon.transform.rotation.x,
				rotationValue,currentCannon.transform.rotation.z,
				currentCannon.transform.rotation.w);
			currentCannon.transform.rotation = q;
			whichSensor = 5;
		}	
		
		if (whichSensor == 2)
		{
			whichSensor = 5;
		}

		#endregion
/////////////////////////////////////////////////////////////////////////////////////////////
////////																		/////////////
////////				C O D E    F O R    T H E    K E Y B O A R D			/////////////
////////																		/////////////
/////////////////////////////////////////////////////////////////////////////////////////////
	
		#region KEYBOARD CODE
		
		if (wait == false && Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.C) { 
			Transform bombSpawn = currentCannon.transform.Find("Barrel").transform.Find("bombSpawn");
			GameObject bomb = Instantiate(bombPrefab, bombSpawn.position, bombSpawn.rotation) as GameObject;
			bomb.rigidbody.AddForce(bombSpawn.forward * currentSpeed, ForceMode.VelocityChange);
			wait = true;			
		}
		
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.UpArrow) {
			Transform barrel = currentCannon.transform.Find("Barrel");
			barrel.transform.Rotate(2, 0, 0);
		}
		
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.DownArrow) {
			Transform barrel = currentCannon.transform.Find("Barrel");
			barrel.transform.Rotate(-2, 0, 0);

		}
			
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.LeftArrow) {
			currentCannon.transform.Rotate(0, 3, 0);
		}
		
		
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.RightArrow) {
			currentCannon.transform.Rotate(0, -3, 0);
		}
		
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Z) {
			DrawGameOver(0);
		}

		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.N) {
			if (currentPlayer == 0) {
				speed0 -= 2;
				if (speed0 < 0) speed0 = 0;
				currentSpeed = speed0;
			}
			else {
				speed1 -= 2;
				if (speed1 < 0) speed1 = 0;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.M) {
			if (currentPlayer == 0) {
				speed0 += 2;
				if (speed0 > 50) speed0 = 50;
				currentSpeed = speed0;
			}
			else {
				speed1 += 2;
				if (speed1 > 50) speed1 = 50;
				currentSpeed = speed1;
			}
		}
		
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad0) {
			if (currentPlayer == 0) {
				speed0 = 0;
				currentSpeed = speed0;
			}
			else {
				speed1 = 0;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad2) {
			if (currentPlayer == 0) {
				speed0 = 10;
				currentSpeed = speed0;
			}
			else {
				speed1 = 10;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad4) {
			if (currentPlayer == 0) {
				speed0 = 20;
				currentSpeed = speed0;
			}
			else {
				speed1 = 20;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad6) {
			if (currentPlayer == 0) {
				speed0 = 30;
				currentSpeed = speed0;
			}
			else {
				speed1 = 30;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad8) {
			if (currentPlayer == 0) {
				speed0 = 40;
				currentSpeed = speed0;
			}
			else {
				speed1 = 40;
				currentSpeed = speed1;
			}
		}
		if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Keypad9) {
			if (currentPlayer == 0) {
				speed0 = 50;
				currentSpeed = speed0;
			}
			else {
				speed1 = 50;
				currentSpeed = speed1;
			}
		}
		#endregion
		
	}

	private void DrawGameOver(int p) {
		towersPlayer0 = 4;
		towersPlayer1 = 4;
		var style = new GUIStyle();
		style.normal.textColor = Color.blue;
		style.fontSize = 40;
		GUI.Button(new Rect(Screen.width/2-100,Screen.height/2-100, 200, 100), "PLAYER " + (p+1) + " WINS!",style);
	}
      
	
	private void drawSpeed(float speed)
	{
		Vector2 location = new Vector2(400,20);
		Vector2 size = new Vector2(400, 20);
    	GUI.DrawTexture(new Rect(location.x, location.y, size.x, size.y), speedBackground);
		// speed goes from 0 to 40
		float percentage = speed/40;
    	GUI.DrawTexture(new Rect(location.x, location.y, size.x * percentage, size.y), speedProgress);
	}
	
	public void changeTurn() {
		GameObject camera = GameObject.Find("RUISCamera");
		if (currentPlayer == 0) {
			GameObject camera1 = GameObject.Find("Camera1");
			camera.transform.position = camera1.transform.position;
			camera.transform.rotation = camera1.transform.rotation;
			currentPlayer = 1;
		}
		else {
			GameObject camera0 = GameObject.Find("Camera0");
			camera.transform.position = camera0.transform.position;
			camera.transform.rotation = camera0.transform.rotation;
			currentPlayer = 0;
		}
	}
}
